﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Joyeon.Utils
{
    /// <summary>
    /// 用于验证文本框的数字输入是否在指定的范围内，如果超出了该指定范围就不允许输入。
    /// </summary>
    public static class ValidateUtil
    {
        /// <summary>
        /// 通过给TextBox添加PreviewTextInput 事件来验证输入后的TextBox值是否在最大值和最小值范围内[min,max]
        /// </summary>
        /// <param name="textBox"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        public static void BindIntValidate(System.Windows.Controls.TextBox textBox, int min, int max, bool autoSelectAll = false)
        {
            if (textBox == null)
            {
                throw new ArgumentNullException("TextBox为空！");
            }
            BindDoubleValidate(textBox, min, max, autoSelectAll);
        }

        /// <summary>
        /// 通过给TextBox添加PreviewTextInput 事件来验证输入后的TextBox值是否在最大值和最小值范围内[min,max]
        /// </summary>
        /// <param name="textBox"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        public static void BindFloatValidate(System.Windows.Controls.TextBox textBox, float min, float max, bool autoSelectAll = false)
        {
            if (textBox == null)
            {
                throw new ArgumentNullException("TextBox为空！");
            }
            BindDoubleValidate(textBox, min, max, autoSelectAll);
        }

        /// <summary>
        /// 通过给TextBox添加PreviewTextInput 事件来验证输入后的TextBox值是否在最大值和最小值范围内[min,max]
        /// </summary>
        /// <param name="textBox"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <param name="autoSelectAll">当输入超出指定范围时是否自动全选已输入内容</param>
        public static void BindDoubleValidate(System.Windows.Controls.TextBox textBox, double min, double max, bool autoSelectAll = false)
        {
            if (textBox == null)
            {
                throw new ArgumentNullException("TextBox为空！");
            }
            textBox.PreviewTextInput += new System.Windows.Input.TextCompositionEventHandler((object sender, System.Windows.Input.TextCompositionEventArgs e) =>
            {
                System.Windows.Controls.TextBox tb = sender as System.Windows.Controls.TextBox;
                String selectedText = tb.SelectedText;
                String validText = tb.Text;
                String text = "";
                if (!String.IsNullOrEmpty(selectedText))
                {
                    text = validText.Replace(selectedText, e.Text);
                }
                else
                {
                    text = validText + e.Text;
                }
                double d = 0;
                bool result = double.TryParse(text, out d);
                if (result == false || d < min || d > max)
                {
                    e.Handled = true;
                    if (autoSelectAll)
                    {
                        tb.SelectAll();
                        tb.Focus();
                    }
                }
            });
        }

    }
    /// <summary>
    /// 验证的类型不属于可验证的类型时会抛出该异常。
    /// </summary>
    public class UnSupportedValidateTypeException : Exception
    {
        public UnSupportedValidateTypeException() : base("验证验证类型不支持！") { }
    }
}
